home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / jed096_1.zip / SLANG / SRC / SLUNIX.C < prev    next >
C/C++ Source or Header  |  1994-04-26  |  6KB  |  200 lines

  1. /* Unix system calls */
  2. /* 
  3.  * Copyright (c) 1992, 1994 John E. Davis 
  4.  * All rights reserved.
  5.  *
  6.  * Permission is hereby granted, without written agreement and without
  7.  * license or royalty fees, to use, copy, and distribute this
  8.  * software and its documentation for any purpose, provided that the
  9.  * above copyright notice and the following two paragraphs appear in
  10.  * all copies of this software.
  11.  *
  12.  * IN NO EVENT SHALL JOHN E. DAVIS BE LIABLE TO ANY PARTY FOR DIRECT,
  13.  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
  14.  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF JOHN E. DAVIS
  15.  * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16.  *
  17.  * JOHN E. DAVIS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
  18.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  19.  * PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"
  20.  * BASIS, AND JOHN E. DAVIS HAS NO OBLIGATION TO PROVIDE MAINTENANCE,
  21.  * SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  22.  */
  23.  
  24.  
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #include <signal.h>
  28. #include <time.h>
  29.  
  30. #ifndef sequent
  31. # include <unistd.h>
  32. #endif
  33.  
  34. #include <errno.h>
  35. #include <string.h>
  36.  
  37. #include "slang.h"
  38.  
  39. /* map value of errno to standard slang return values. */
  40. static int map_errno(void)
  41. {
  42.    switch (errno)
  43.      {
  44.       case EACCES: return -1;           /* insufficient privilege */
  45.       case ENOENT: return -2;           /* invalid path */
  46.       default: return -50;
  47.      }
  48. }
  49.  
  50.  
  51. #define EQS(a, b) ((*(a) == *(b)) && (!strcmp(a + 1, b + 1)))
  52.  
  53. static struct stat stat_buf;
  54.  
  55. static int parse_stat(struct stat *s, char *f)
  56. {
  57.    if (!strncmp(f, "st_", 3)) f += 3;
  58.    
  59.    if (EQS("dev", f)) return s->st_dev;
  60.    if (EQS("ino", f)) return s->st_ino;
  61.    if (EQS("mode", f)) return s->st_mode;
  62.    if (EQS("nlink", f)) return s->st_nlink;
  63.    if (EQS("uid", f)) return s->st_uid;
  64.    if (EQS("gid", f)) return s->st_gid;
  65.    if (EQS("rdev", f)) return s->st_rdev;
  66.    if (EQS("size", f)) return s->st_size;
  67.    if (EQS("atime", f)) return s->st_atime;
  68.    if (EQS("mtime", f)) return s->st_mtime;
  69.    if (EQS("ctime", f)) return s->st_ctime;
  70.    SLang_doerror("Unknown stat field.");
  71.    return (0);
  72. }
  73.  
  74.  
  75. static int unix_stat_file(char *file)
  76. {
  77.    if (!stat (file, &stat_buf)) return 0;
  78.    return map_errno();
  79. }
  80.  
  81. #ifndef __GO32__
  82. static int unix_lstat_file(char *file)
  83. {
  84.    if (!lstat (file, &stat_buf)) return 0;
  85.    return map_errno();
  86. }
  87. #endif
  88.  
  89. static int unix_stat_struct(char *field)
  90. {
  91.    return parse_stat(&stat_buf, field);
  92. }
  93.  
  94. static int unix_chmod(char *file, int *mode)
  95. {
  96.    if (!chmod(file, (mode_t) *mode)) return 0;
  97.    return map_errno();
  98. }
  99.  
  100. static int unix_chown(char *file, int *owner, int *group)
  101. {
  102.    if (!chown(file, (uid_t) *owner, (gid_t) *group)) return 0;
  103.    return map_errno();
  104. }
  105.  
  106. /* This is mainly designed to check pids, but who knows.... */
  107. static int unix_kill(void)
  108. {
  109.    int pid, sig;
  110.    
  111.    if (SLang_pop_integer(&sig) || SLang_pop_integer(&pid)) return (-1);
  112.    return kill ((pid_t) pid, sig);
  113. }
  114.  
  115.  
  116. static char *unix_ctime(int *tt)
  117. {
  118.    char *t;
  119.    
  120.    t = ctime ((time_t *) tt);
  121.    t[24] = 0;  /* knock off \n */
  122.    return (t);
  123. }
  124.  
  125.  
  126. static SLang_Name_Type slunix_table[] =
  127. {
  128.    MAKE_INTRINSIC(".unix_kill", unix_kill, INT_TYPE, 0),
  129.    /*Prototype: Integer unix_kill(Integer pid, Integer sig);
  130.     */
  131.    MAKE_INTRINSIC(".unix_ctime", unix_ctime, STRING_TYPE, 1),
  132.    /*Prototype: String unix_ctime(Integer secs);
  133.      Returns a string representation of the time as given by 'secs' seconds
  134.      since 1970. */
  135. #ifndef __GO32__
  136.    MAKE_INTRINSIC(".lstat_file", unix_lstat_file, INT_TYPE, 1),
  137.    /*Prototype: Integer lstat_file(String file);
  138.      This function is like 'stat_file' but it returns information about 
  139.      the link itself. See 'stat_file' for usage.
  140.      See also: stat_file */
  141. #endif
  142.    MAKE_INTRINSIC(".stat_file", unix_stat_file, INT_TYPE, 1),
  143.    /*Prototype: Integer stat_file(String file);
  144.      This function returns information about 'file' through the use of the 
  145.      system 'stat' call.  If the stat call fails, the function returns a 
  146.      negative integer.  If it is successful, it returns zero.  Upon failure it 
  147.      returns a negative number.
  148.  
  149.      To retrieve information obtained by this call, use the 'stat_struct'
  150.      function.
  151.      See also: lstat_file, stat_struct */
  152.  
  153.    MAKE_INTRINSIC(".stat_struct", unix_stat_struct, INT_TYPE, 1),
  154.    /*Prototype Integer stat_struct(String field);
  155.      This functions returns information previously obtained by a call to the
  156.      'stat_file' or 'lstat_file' functions.  The 'field' argument specifies
  157.      what piece of information to return.   Valid values for 'field' are:
  158.      
  159.          "dev"
  160.      "ino"
  161.      "mode"
  162.      "nlink"
  163.      "uid"
  164.      "gid"
  165.      "rdev"
  166.      "size"
  167.      "atime"
  168.      "mtime"
  169.      "ctime"
  170.      
  171.     See the man page for 'stat' for a discussion of these fields.
  172.     The following example returns the size of the file "jed.rc":
  173.     
  174.         variable size;
  175.         if (stat_file("jed.rc") < 0) error ("Unable to stat file!");
  176.     size = stat_struct("size");
  177.     */
  178.  
  179.    MAKE_INTRINSIC(".chown", unix_chown, INT_TYPE, 3),
  180.    /*Prototype Integer chown(String file, Integer uid, Integer gid);
  181.      Change ownership of 'file' to that of user id 'uid' and group id 'gid'.
  182.      This function returns 0 upon success and a negative number up failure.
  183.      It returns -1 if the process does not have sufficent privileges and -2
  184.      if the file does not exist. 
  185.      See also: chmod, stat_file */
  186.    MAKE_INTRINSIC(".chmod", unix_chmod, INT_TYPE, 2),
  187.    /*Prototype Integer chmod(String file, Integer mode);
  188.      'chmod' changes the permissions of 'file' to those specified by 'mode'.
  189.      It returns 0 upon success, -1 if the process lacks sufficient privilege
  190.      for the operation, or -2 if the file does not exist.
  191.      See also: chown, stat_file
  192.      */
  193.    SLANG_END_TABLE
  194. };
  195.  
  196. int init_SLunix()
  197. {
  198.    return SLang_add_table(slunix_table, "_Unix");
  199. }
  200.